import 'dart:io';

import 'package:android_id/android_id.dart';
import 'package:date_format/date_format.dart';
import 'package:device_info_plus/device_info_plus.dart';
import 'package:flutter/services.dart';
import 'package:package_info_plus/package_info_plus.dart';

import 'log_utils.dart';

class PhoneInfoUtils {
  ///包信息
  static PackageInfo? _packageInfo;

  ///安卓设备信息
  static AndroidDeviceInfo? _androidInfo;

  ///ios设备信息
  static IosDeviceInfo? _iosInfo;

  static String? deviceId;

  ///初始化包信息
  static Future<void> initPackageInfo() async {
    _packageInfo = await PackageInfo.fromPlatform();
  }

  ///初始化设备信息
  static Future<void> initDeviceInfo() async {
    final DeviceInfoPlugin deviceInfoPlugin = DeviceInfoPlugin();

    if (Platform.isAndroid) {
      _androidInfo = await deviceInfoPlugin.androidInfo;

      /// 获取Android设备唯一编号  终端唯一标识
      const androidIdPlugin = AndroidId();
      try {
        deviceId = await androidIdPlugin.getId() ?? 'Unknown ID';
      } on PlatformException {
        deviceId = 'Failed to get Android ID.';
      }
    } else {
      _iosInfo = await deviceInfoPlugin.iosInfo;

      /// 获取IOS设备唯一编号  终端唯一标识
      var data = await deviceInfoPlugin.iosInfo;
      deviceId = data.identifierForVendor; // iOS
    }
  }

  ///app接口内使用
  ///打印包信息
  static PackageInfo? getPackageInfo() {
    //APP名称
    String? appName = _packageInfo?.appName;
    //包名
    String? packageName = _packageInfo?.packageName;
    //版本名
    String? version = _packageInfo?.version;
    //版本号
    String? buildNumber = _packageInfo?.buildNumber;

    // LogUtils.showLog("getPackageInfo:\n"
    //     "APP名称=$appName\n"
    //     "包名=$packageName\n"
    //     "版本名=$version\n"
    //     "版本号=$buildNumber");
    return _packageInfo;
  }

  ///打印安卓设备信息
  static AndroidDeviceInfo? getAndroidDeviceInfo() {
    //系统相关信息
    String? baseOS = _androidInfo?.version.baseOS;
    String? codename = _androidInfo?.version.codename;
    String? incremental = _androidInfo?.version.incremental;
    String? previewSdkInt = _androidInfo?.version.previewSdkInt.toString();
    String? release = _androidInfo?.version.release;
    String? sdkInt = _androidInfo?.version.sdkInt.toString();
    String? securityPatch = _androidInfo?.version.securityPatch;

    //手机品牌
    String? brand = _androidInfo?.brand;
    //手机型号
    String? device = _androidInfo?.device;
    //硬件信息
    String? hardware = _androidInfo?.hardware;

    String? deviceid = deviceId;

    // LogUtils.showLog("getAndroidDeviceInfo:\n"
    //     "baseOS=$baseOS\n"
    //     "codename=$codename\n"
    //     "incremental=$incremental\n"
    //     "previewSdkInt=$previewSdkInt\n"
    //     "release=$release\n"
    //     "sdkInt=$sdkInt\n"
    //     "securityPatch=$securityPatch\n"
    //     "手机品牌=$brand\n"
    //     "手机型号=$device\n"
    //     "硬件信息=$hardware\n"
    //     "终端唯一标识=$deviceid"
    // );

//    getAndroidDeviceInfo:
//    baseOS=
//    codename=REL
//    incremental=compiler03171219
//    previewSdkInt=0
//    release=9
//    sdkInt=28
//    securityPatch=2021-05-01
//    手机品牌=vivo
//    手机型号=PD1809
//    硬件信息=qcom

    return _androidInfo;
  }

  ///打印苹果设备信息
  static IosDeviceInfo? getIosDeviceInfo() {
    //系统相关信息
    String? name = _iosInfo?.name;
    String? systemName = _iosInfo?.systemName;
    String? systemVersion = _iosInfo?.systemVersion;
    String? model = _iosInfo?.model;
    String? localizedModel = _iosInfo?.localizedModel;
    String? identifierForVendor = _iosInfo?.identifierForVendor;

    // LogUtils.showLog("getIosDeviceInfo:\n"
    //     "name=$name\n"
    //     "systemName=$systemName\n"
    //     "systemVersion=$systemVersion\n"
    //     "model=$model\n"
    //     "localizedModel=$localizedModel\n"
    //     "identifierForVendor=$identifierForVendor");

//    getIosDeviceInfo:
//    name=iPhone 12
//    systemName=iOS
//    systemVersion=14.5
//    model=iPhone
//    localizedModel=iPhone
//    identifierForVendor=C03F0455-3341-4ED2-8166-DA8BD2861B3C

    return _iosInfo;
  }

  ///获取安装包版本号
  static String getAppVersion() {
    return PhoneInfoUtils.getPackageInfo() != null
        ? PhoneInfoUtils.getPackageInfo()!.version
        : "1.0.0";
  }

  ///获取设备品牌型号
  static String getDeviceModel() {
    if (Platform.isAndroid) {
      return PhoneInfoUtils.getAndroidDeviceInfo() != null
          ? "手机品牌：${PhoneInfoUtils.getAndroidDeviceInfo()!.brand}"
              "-手机型号：${PhoneInfoUtils.getAndroidDeviceInfo()!.device}"
          : "DeviceModel Android";
    } else {
      return PhoneInfoUtils.getIosDeviceInfo() != null
          ? "手机品牌：Apple-手机型号：${PhoneInfoUtils.getIosDeviceInfo()!.name}"
          : "DeviceModel Apple";
    }
  }

  ///获取设备系统版本号
  static String getDeviceSystem() {
    if (Platform.isAndroid) {
      return PhoneInfoUtils.getAndroidDeviceInfo() != null
          ? "系统SDK版本号：${PhoneInfoUtils.getAndroidDeviceInfo()!.version.sdkInt}"
          : "DeviceSystem Android";
    } else {
      return PhoneInfoUtils.getIosDeviceInfo() != null
          ? "系统SDK版本号：${PhoneInfoUtils.getIosDeviceInfo()!.systemName}"
              "-${PhoneInfoUtils.getIosDeviceInfo()!.systemVersion}"
          : "DeviceSystem Apple";
    }
  }

  ///获取时间戳
  static String getTimeStamp() {
    return formatDate(
            DateTime.now(), [yyyy, '-', mm, '-', dd, " ", HH, ':', nn, ':', ss])
        .toString();
  }

  ///获取appid
  static String getAppId() {
    return Platform.isAndroid ? '100001' : '100002';
  }

  static String getDate() {
    return formatDate(DateTime.now(), [yyyy, '-', mm, '-', dd]).toString();
  }

  static String getTime() {
    return formatDate(DateTime.now(), [HH, ':', nn, ':', ss]).toString();
  }
}
